package cn.hallele.leinsteining.ms.support.starter.components.plugin.initializer;

import cn.hallele.leinsteining.ms.infrastructure.plugin.safety.encryptor.AESEncryptor;
import jakarta.annotation.Nonnull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * PluginContextInitializer
 *
 * @author anle5
 * @since 2024/7/28 15:37
 */
@Slf4j
public class PluginContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    /**
     * Initialize the given application context.
     *
     * @param applicationContext the application to configure
     */
    @Override
    public void initialize(@Nonnull ConfigurableApplicationContext applicationContext) {
        initEncryptor(applicationContext);
    }

    /**
     * 提前注入Encryptor，以便bootstrap.yml加载时能取道
     *
     * @param applicationContext applicationContext
     */
    private void initEncryptor(ConfigurableApplicationContext applicationContext) {
        String name = AESEncryptor.class.getSimpleName();
        applicationContext.getBeanFactory().registerSingleton(name, new AESEncryptor());
        log.info("Init customer Encryptor: {}", name);
    }
}
